package com.demo.base.resolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.demo.base.anno.CurrentUser;
import com.demo.base.consant.AppConsant;
import com.demo.base.user.UserInfoVO;

/**
 * 用户信息
 */
public class CurrentUserResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(CurrentUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        if (methodParameter.getParameterType().equals(UserInfoVO.class)) {
            HttpSession httpSession = nativeWebRequest.getNativeRequest(HttpServletRequest.class).getSession();
            return (UserInfoVO)httpSession.getAttribute(AppConsant.CURRENT_USER);
        }
        return null;
    }
}